<?php

namespace api\modules\v1\controllers\rbac;

use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\ActionForm;
use common\models\member\Member;
use common\models\rbac\Action;
use common\helpers\CommonHelper;
use services\rbac\ActionService;
use Yii;


class ActionController extends OnAuthController
{
    /**
     * @var \common\models\rbac\Action
     */
    public $modelClass = Action::class;

    public function beforeAction($action)
    {
        $res = parent::beforeAction($action); // TODO: Change the autogenerated stub

        if (Yii::$app->user->identity->member->super_type !== Member::SUPER_TYPE_SUPER_ADMIN) {
            CommonHelper::api(422, '无权限操作');
            return false;
        }
        return $res;
    }

    /**
     * @api {get} /v1/rbac/action/list 动作列表
     * @apiName list
     * @apiGroup RbacAction
     * @apiUse CommonParam
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   列表
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $actionService = new ActionService();
        $data = $actionService->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化
        $data['list'] = $actionService->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/action/info 动作详情
     * @apiName info
     * @apiGroup RbacAction
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                        动作ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Number}  data.id                动作ID
     * @apiSuccess {Number}  data.menu_id           菜单ID
     * @apiSuccess {Number}  data.module_id         模块ID
     * @apiSuccess {String}  data.name              动作名称
     * @apiSuccess {String}  data.flag              动作路由
     * @apiSuccess {Number}  data.sort              排序
     * @apiSuccess {Number}  data.status            状态
     * @apiSuccess {Array}   data.module_list       全部模块列表【循环】
     * @apiSuccess {Number}  data.module_list.id    模块ID
     * @apiSuccess {String}  data.module_list.name  模块名
     * @apiSuccess {Object}  data.menu_list         全部菜单列表【循环  module_id => []】
     * @apiSuccess {Number}  data.menu_list.id      菜单ID
     * @apiSuccess {String}  data.menu_list.name    菜单名
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $actionService = new ActionService();
        return $actionService->getInfo((int)$id);
    }

    /**
     * @api {post} /v1/rbac/action/create 新增动作
     * @apiName create
     * @apiGroup RbacAction
     * @apiUse CommonParam
     *
     * @apiBody {String} name              动作名称
     * @apiBody {Number} menu_id           菜单ID
     * @apiBody {String} flag              动作路由
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ActionForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $actionService = new ActionService();
        return $actionService->create($model);
    }

    /**
     * @api {post} /v1/rbac/action/update 编辑动作
     * @apiName update
     * @apiGroup RbacAction
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                动作ID
     * @apiBody {String} name              动作名称
     * @apiBody {Number} menu_id           菜单ID
     * @apiBody {String} flag              动作路由
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new ActionForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $actionService = new ActionService();
        return $actionService->edit($model);
    }

    /**
     * @api {post} /v1/rbac/action/delete 删除动作
     * @apiName delete
     * @apiGroup RbacAction
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                动作ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         动作ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();
        $params['member_id'] = Yii::$app->user->identity->member_id;

        $model = new ActionForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $actionService = new ActionService();
        return $actionService->delete($model);
    }
}
